package com.eco.fanliapp.admin.controller.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.SyModule;
import com.eco.fanliapp.entity.SyModuleChildren;
import com.eco.fanliapp.entity.SyPageSetup;
import com.eco.fanliapp.service.ISyModuleChildrenService;
import com.eco.fanliapp.service.ISyModuleService;
import com.eco.fanliapp.service.RedisTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 模块表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-09
 */
@RestController
@RequestMapping("/web/syModule")
public class SyModuleController extends BaseController {

    @Autowired
    private ISyModuleChildrenService iSyModuleChildrenService;

    @Autowired
    private ISyModuleService iSyModuleService;

    @Autowired
    private RedisTemplateService redisTemplateService;


    @GetMapping(value = "list")
    public ServerResponse list(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted",0);
        List<SyModule> syModuleList = iSyModuleService.list(queryWrapper);
        List<SyModuleChildren> syModuleChildrenList = iSyModuleChildrenService.list(queryWrapper);
        for(SyModule syModule :syModuleList){
            List<SyModuleChildren> oneSyModuleChildrenList = new ArrayList<>();
            for (SyModuleChildren syModuleChildren:syModuleChildrenList){
                if(syModule.getTableId() == syModuleChildren.getModuleId()){
                    oneSyModuleChildrenList.add(syModuleChildren);
                    continue;
                }
            }
            syModule.setSyModuleChildrenList(oneSyModuleChildrenList);
        }
        return  ServerResponse.createBySuccess("查询成功",syModuleList);
    }

    @PostMapping(value = "update")
//    @Transactional
    @CacheEvict(value={"moneyTutorial","kingKongList"},  beforeInvocation=true)
    @SysLogAnnotation(value = "修改了金刚区",userType = 2)
    public ServerResponse update(@RequestBody SyModule syModule){
        //boolean syModuleNum = iSyModuleService.saveOrUpdate(syModule);
        List<SyModuleChildren> syModuleChildrenList = syModule.getSyModuleChildrenList();
        boolean syModuleChildrenNum = false;
        if(!CollectionUtils.isEmpty(syModuleChildrenList)){
            for (SyModuleChildren syModuleChildren :syModuleChildrenList){
                syModuleChildren.setModuleId(syModule.getTableId());
            }
             syModuleChildrenNum = iSyModuleChildrenService.saveOrUpdateBatch(syModuleChildrenList);
        }
        if(syModuleChildrenNum /*&& syModuleNum*/){
            //删除缓存
            redisTemplateService.del("app_module_kingkong_list");
            return  ServerResponse.createBySuccessMessage("操作成功");
        }
        return  ServerResponse.createByErrorMessage("操作失败");

    }
   /* @GetMapping(value = "onOff")
    public ServerResponse moduleOnOff(Integer tableId,Boolean onOff){
        return  iSyModuleService.moduleOnOff(tableId,onOff);

    }*/

    @PostMapping(value = "onOff")
//    @Transactional
    @SysLogAnnotation(value = "修改了开关",userType = 2)
    public ServerResponse update(@RequestBody List<SyModule> syModuleList){
        boolean syModuleNum = iSyModuleService.updateBatchById(syModuleList);
        if(syModuleNum){
            redisTemplateService.del("app_switch_all");
            return  ServerResponse.createBySuccessMessage("操作成功");
        }
        return  ServerResponse.createByErrorMessage("操作失败");

    }
}
